import sys
sys.setrecursionlimit(10000000)
def dfs_sea(i, j):  # 遍历外海
    # 检查坐标是否在范围内，且当前格子是否为海水
    if not 0 <= i <= 51 or not 0 <= j <= 51 or lst[i][j] != 0:
        return

    lst[i][j] = 2  # 给外海染上2的颜色
    # 外海蔓延八个方向遍历
    dfs_sea(i + 1, j)
    dfs_sea(i - 1, j)
    dfs_sea(i, j + 1)
    dfs_sea(i, j - 1)
    dfs_sea(i - 1, j + 1)
    dfs_sea(i - 1, j - 1)
    dfs_sea(i + 1, j + 1)
    dfs_sea(i + 1, j - 1)

def dfs_land(i, j):  # 陆地
    if not 0 <= i <= 51 or not 0 <= j <= 51 or lst[i][j] != 1:
        return

    lst[i][j] = 3
    dfs_land(i + 1, j)
    dfs_land(i - 1, j)
    dfs_land(i, j + 1)
    dfs_land(i, j - 1)

def isok(row, col):  # 判断当前陆地格子是否与外海相邻
    if lst[row - 1][col] == 2: return True
    if lst[row + 1][col] == 2: return True
    if lst[row][col + 1] == 2: return True
    if lst[row][col - 1] == 2: return True
    return False

t = int(input())  # t组测试数据
for i in range(t):
    m, n = map(int, input().split())
    # m和n的数据都在1-50的范围内，直接生成一个0,1,2,3，...，51 的数组
    lst = [[0] * 52 for i in range(52)]
    # 地图数据被 0 值包围
    for i in range(m):
        tmp = input()
        for j in range(n):
            lst[i + 1][j + 1] = int(tmp[j])

    dfs_sea(0, 0)  # 给外海染色
    ans = 0
    for row in range(52):
        for col in range(52):
            if lst[row][col] == 1 and isok(row, col):
                dfs_land(row, col)
                ans += 1
    print(ans)


